package HibernateUtilsforBM;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

/**
 * 用于初始化数据库连接池
 * 服务器停止的时候会销毁
 * Application Lifecycle Listener implementation class DBConnectorPoolListener
 *
 */
@WebListener
public class DBConnectorPoolListener implements ServletContextListener {

    /**
     * Default constructor. 
     */
    public DBConnectorPoolListener() {
        // TODO Auto-generated constructor stub
    }



	/**
	 * 初始化listener
     * @see ServletContextListener#contextInitialized(ServletContextEvent)
     */
    public void contextInitialized(ServletContextEvent arg0)  { 
         // TODO Auto-generated method stub
    	System.out.println("初始化web应用...");
    	Configuration cfg = new Configuration().configure();
    	SessionFactory factory = cfg.buildSessionFactory();
    	SFactoryGenerator.setFactory(factory);
    }



	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		// TODO Auto-generated method stub
		System.out.println("web应用关闭中...");
		SFactoryGenerator.getFactory().close();
	}
	
}
